package com.tony.jdbc.dynamic.datasource;

/**
 * @author tony
 * @desc 数据源连接信息多线程切换缓存
 * @date 2025/11/12 08:51
 */
public final class DynamicDataSourceKeyHolder {
    public static final String JDBC_TEMPLATE = "_jdbcTemplate";
    public static final String NAMED_JDBC_TEMPLATE = "_namedJdbcTemplate";
    /**
     * 线程变量，线程内有效
     */
    private static final ThreadLocal<String> dataSourceKey = new ThreadLocal<>();

    private DynamicDataSourceKeyHolder() {
    }

    /**
     * @return 线程key
     */
    public static String getKey() {
        return dataSourceKey.get();
    }

    /**
     * 设置多线程下数据源
     *
     * @param key 数据源连接信息
     */
    public static void setKey(String key) {
        dataSourceKey.set(key);
    }

    /**
     * 移除当前线程数据源key
     */
    public static void removeKey() {
        dataSourceKey.remove();
    }
}
